package com.pig4cloud.pig.village.reward.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.village.api.dto.RewardInfoDTO;
import com.pig4cloud.pig.village.api.request.*;
import com.pig4cloud.pig.village.api.vo.BASE64DecodedMultipartFile;
import com.pig4cloud.pig.village.reward.service.IRewardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author limbray.li
 * @date 2021/6/26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/reward")
@Api(value = "reward", tags = "红黑榜")
public class RewardController {
	@Resource
	private IRewardService rewardService;

	@ApiOperation(value = "查询")
	@GetMapping("/page")
	public R<IPage<RewardInfoDTO>> page(Integer pageNum, Integer pageSize, RewardSearch search) {
		return R.ok(rewardService.pageBySearch(new Page(pageNum, pageSize), search));
	}

	@ApiOperation(value = "提交")
	@PostMapping(value = "/add", headers = "content-type=multipart/form-data")
	public R add(RewardNoFiles reward
			, @ApiParam(value = "文件", required = true) @RequestPart("sign") MultipartFile sign
			, @ApiParam(value = "文件", required = true) @RequestPart("files") MultipartFile[] files) {
		RewardAdd rewardAdd = new RewardAdd();
		BeanUtil.copyProperties(reward, rewardAdd);
		rewardAdd.setSign(sign);
		rewardAdd.setFiles(files);
		return R.ok(rewardService.addReward(rewardAdd));
	}

	@ApiOperation(value = "提交-base64")
	@PostMapping(value = "/addWithBase64")
	public R addWithBase64(@RequestBody RewardWithBase64File reward) {
		RewardAdd rewardAdd = new RewardAdd();
		BeanUtil.copyProperties(reward, rewardAdd);
		rewardAdd.setSign(BASE64DecodedMultipartFile.base64ToMultipart(reward.getBase64Sign()));
		MultipartFile[] multipartFiles = reward.getBase64Files().stream().map(e->BASE64DecodedMultipartFile.base64ToMultipart(e)).toArray(MultipartFile[]::new);
		rewardAdd.setFiles(multipartFiles);
		return R.ok(rewardService.addReward(rewardAdd));
	}
	@ApiOperation(value = "修改")
	@PutMapping(value = "/update", headers = "content-type=multipart/form-data")
	public R update(RewardNoFiles reward
			, @ApiParam(value = "id", required = true) Long rewardId
			, @ApiParam(value = "事件id", required = true) Long eventId
			, @ApiParam(value = "文件", required = true) @RequestPart("sign") MultipartFile sign
			, @ApiParam(value = "文件", required = true) @RequestPart("files") MultipartFile[] files) {
		if (rewardId == null || eventId == null) {
			return R.failed("缺少必要参数！");
		}
		RewardUpdate rewardUpdate = new RewardUpdate();
		BeanUtil.copyProperties(reward, rewardUpdate);
		rewardUpdate.setSign(sign);
		rewardUpdate.setFiles(files);
		rewardUpdate.setId(rewardId);
		rewardUpdate.setEventId(eventId);
		return R.ok(rewardService.updateReward(rewardUpdate));
	}

	@ApiOperation(value = "修改-base64")
	@PutMapping(value = "/updateWithBase64")
	public R updateWithBase64(@RequestBody RewardUpdateWithBase64File reward) {
		if (reward.getRewardId() == null || reward.getEventId() == null) {
			return R.failed("缺少必要参数！");
		}
		RewardUpdate rewardUpdate = new RewardUpdate();
		BeanUtil.copyProperties(reward, rewardUpdate);
		rewardUpdate.setId(reward.getRewardId());
		rewardUpdate.setSign(BASE64DecodedMultipartFile.base64ToMultipart(reward.getBase64Sign()));
		MultipartFile[] multipartFiles = reward.getBase64Files().stream().map(e->BASE64DecodedMultipartFile.base64ToMultipart(e)).toArray(MultipartFile[]::new);
		rewardUpdate.setFiles(multipartFiles);
		return R.ok(rewardService.updateReward(rewardUpdate));
	}

	@ApiOperation(value = "审核通过")
	@PutMapping(value = "/access")
	public R access(@ApiParam(value = "id", required = true) Long rewardId) {
		if (rewardId == null) {
			return R.failed("缺少必要参数！");
		}
		String username = SecurityUtils.getUser().getUsername();
		return R.ok(rewardService.accessReward(rewardId, username));
	}
}
